﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//Please add references
namespace SCS.DAL
{
	/// <summary>
	/// 数据访问类:SCS_Receipt_Detail
	/// </summary>
	public partial class SCS_Receipt_Detail
	{
		public SCS_Receipt_Detail()
		{}
		#region  Method



		/// <summary>
		/// 增加一条数据
		/// </summary>
		public bool Add(SCS.Model.SCS_Receipt_Detail model)
		{
			StringBuilder strSql=new StringBuilder();
			StringBuilder strSql1=new StringBuilder();
			StringBuilder strSql2=new StringBuilder();
			if (model.Receipt_ID != null)
			{
				strSql1.Append("Receipt_ID,");
				strSql2.Append("'"+model.Receipt_ID+"',");
			}
			if (model.Standard_ID != null)
			{
				strSql1.Append("Standard_ID,");
				strSql2.Append(""+model.Standard_ID+",");
			}
			if (model.Receipt_Fee_Payable != null)
			{
				strSql1.Append("Receipt_Fee_Payable,");
				strSql2.Append(""+model.Receipt_Fee_Payable+",");
			}
			if (model.Receipt_Real_Fees != null)
			{
				strSql1.Append("Receipt_Real_Fees,");
				strSql2.Append(""+model.Receipt_Real_Fees+",");
			}
			if (model.Receipt_Mark != null)
			{
				strSql1.Append("Receipt_Mark,");
				strSql2.Append("'"+model.Receipt_Mark+"',");
			}
			strSql.Append("insert into SCS_Receipt_Detail(");
			strSql.Append(strSql1.ToString().Remove(strSql1.Length - 1));
			strSql.Append(")");
			strSql.Append(" values (");
			strSql.Append(strSql2.ToString().Remove(strSql2.Length - 1));
			strSql.Append(")");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(SCS.Model.SCS_Receipt_Detail model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update SCS_Receipt_Detail set ");
			if (model.Receipt_ID != null)
			{
				strSql.Append("Receipt_ID='"+model.Receipt_ID+"',");
			}
			else
			{
				strSql.Append("Receipt_ID= null ,");
			}
			if (model.Standard_ID != null)
			{
				strSql.Append("Standard_ID="+model.Standard_ID+",");
			}
			else
			{
				strSql.Append("Standard_ID= null ,");
			}
			if (model.Receipt_Fee_Payable != null)
			{
				strSql.Append("Receipt_Fee_Payable="+model.Receipt_Fee_Payable+",");
			}
			else
			{
				strSql.Append("Receipt_Fee_Payable= null ,");
			}
			if (model.Receipt_Real_Fees != null)
			{
				strSql.Append("Receipt_Real_Fees="+model.Receipt_Real_Fees+",");
			}
			else
			{
				strSql.Append("Receipt_Real_Fees= null ,");
			}
			if (model.Receipt_Mark != null)
			{
				strSql.Append("Receipt_Mark='"+model.Receipt_Mark+"',");
			}
			else
			{
				strSql.Append("Receipt_Mark= null ,");
			}
			int n = strSql.ToString().LastIndexOf(",");
			strSql.Remove(n, 1);
			strSql.Append(" where ");
			int rowsAffected=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rowsAffected > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete()
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from SCS_Receipt_Detail ");
			strSql.Append(" where " );
			int rowsAffected=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rowsAffected > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public SCS.Model.SCS_Receipt_Detail GetModel()
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1  ");
			strSql.Append(" Receipt_ID,Standard_ID,Receipt_Fee_Payable,Receipt_Real_Fees,Receipt_Mark ");
			strSql.Append(" from SCS_Receipt_Detail ");
			strSql.Append(" where " );
			SCS.Model.SCS_Receipt_Detail model=new SCS.Model.SCS_Receipt_Detail();
			DataSet ds=DbHelperSQL.Query(strSql.ToString());
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public SCS.Model.SCS_Receipt_Detail DataRowToModel(DataRow row)
		{
			SCS.Model.SCS_Receipt_Detail model=new SCS.Model.SCS_Receipt_Detail();
			if (row != null)
			{
				if(row["Receipt_ID"]!=null)
				{
					model.Receipt_ID=row["Receipt_ID"].ToString();
				}
				if(row["Standard_ID"]!=null && row["Standard_ID"].ToString()!="")
				{
					model.Standard_ID=int.Parse(row["Standard_ID"].ToString());
				}
				if(row["Receipt_Fee_Payable"]!=null && row["Receipt_Fee_Payable"].ToString()!="")
				{
					model.Receipt_Fee_Payable=decimal.Parse(row["Receipt_Fee_Payable"].ToString());
				}
				if(row["Receipt_Real_Fees"]!=null && row["Receipt_Real_Fees"].ToString()!="")
				{
					model.Receipt_Real_Fees=decimal.Parse(row["Receipt_Real_Fees"].ToString());
				}
				if(row["Receipt_Mark"]!=null)
				{
					model.Receipt_Mark=row["Receipt_Mark"].ToString();
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select Receipt_ID,Standard_ID,Receipt_Fee_Payable,Receipt_Real_Fees,Receipt_Mark ");
			strSql.Append(" FROM SCS_Receipt_Detail ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" Receipt_ID,Standard_ID,Receipt_Fee_Payable,Receipt_Real_Fees,Receipt_Mark ");
			strSql.Append(" FROM SCS_Receipt_Detail ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM SCS_Receipt_Detail ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.Receipt_ID desc");
			}
			strSql.Append(")AS Row, T.*  from SCS_Receipt_Detail T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		*/

		#endregion  Method
		#region  MethodEx

		#endregion  MethodEx
	}
}

